package com.example.springboottestgit.controller;

import com.example.springboottestgit.bean.CommonException;
import com.example.springboottestgit.bean.ResultMsg;
import com.example.springboottestgit.bean.dto.CommonDTO;
import com.example.springboottestgit.bean.vo.CommonVO;
import com.example.springboottestgit.enums.ExceptionCodeEnum;
import com.example.springboottestgit.service.DocumentQAService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @description:通义千问文档问答
 * @author: ZZZ
 */
@RestController
public class DocumentQAController {
    @Autowired
    private DocumentQAService documentQAService;

    @PostMapping("/qwen/file/upload")
    public ResultMsg<CommonVO> fileUpload(@RequestParam("file") MultipartFile multipartFile) {
        String fileName = multipartFile.getOriginalFilename();
        if (!fileName.endsWith(".txt")) {
            throw new CommonException("001001", "文件格式错误，请上传 txt 文件");
        }
        CommonVO vo = documentQAService.fileUpload(multipartFile);
        return ResultMsg.success(vo);
    }


    @PostMapping("/qwen/document/content/qa")
    public ResultMsg<CommonVO> documentContentQa(@RequestBody CommonDTO dto) {

        if (!StringUtils.hasText(dto.getQuestion())) {
            throw new CommonException(ExceptionCodeEnum.QUERY_NO_RESULT.code, "question不能为空");
        }

        /*if (dto.getFileId() == null) {
            throw new CommonException(ExceptionCodeEnum.QUERY_NO_RESULT.code, "fileId不能为空");
        }
        */
        CommonVO vo = documentQAService.documentContentQa(dto);
        return ResultMsg.success(vo);
    }

}
